package com.fsbm.ctech.maxis.ebpsbackend;

/******************************************************************************
* All the utility methods that are
* used across the project to be created here
******************************************************************************/

import java.util.Vector;
import java.util.*;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import java.text.SimpleDateFormat;
import java.util.Date;

public class GenUtil
{

////////////////////////////////////////////////////////////////////////////////

	/**
	 * This method is to change the date format from one string to another
	 * @param strDate String to be parsed
	 * @return String parsed string
	 */
	public static String parseDateFormat(String strDate,String origPattern, String newPattern) {
		String returnDate = "";

		try{

			//create SimpleDateFormat object with source string date format
			SimpleDateFormat sdfSource = new SimpleDateFormat(origPattern);

			//parse the string into Date object
			Date date = sdfSource.parse(strDate);

			//create SimpleDateFormat object with desired date format
			SimpleDateFormat sdfDestination = new SimpleDateFormat(newPattern);

			//parse the date into another format
			returnDate = sdfDestination.format(date);
		}catch(Exception pe){
			System.out.println("Exception : " + pe);
		}
		return returnDate;
	}// end parseDateFormat


	/**
	 * This method is to replace the string with empty value
	 * or trim if got the value
	 * @param sString String to be parsed
	 * @return String parsed string
	 */
	public static String replaceNull(String sString) {
		if((sString == null) || (sString.trim().equalsIgnoreCase("null")))
			sString = "";
		else
			sString = sString.trim();

		return sString;
	} // replaceNull

	public static boolean isNumeric(String str) {
	  try
	  {
		double d = Double.parseDouble(str);
	  }catch(NumberFormatException nfe){
		return false;
	  }
	  return true;
	}

	public static String getNoOfPages(int listSize) {
		int maxPerPage = 10;
		int NumPages = listSize/maxPerPage;
		try{

			if ((listSize%maxPerPage) > 0){
				NumPages++;
			}
		}catch (Exception e1) {
			System.out.println("Exception: " + e1.toString());
		}catch (Throwable e2) {
			System.out.println("Export error: " + e2.toString());
		}
		return Integer.toString(NumPages);
	}

	public static String getNoOfPagesByPageNo(int listSize,int maxPerPage) {
		//int maxPerPage = 10;
		int NumPages = listSize/maxPerPage;
		try{

			if ((listSize%maxPerPage) > 0){
				NumPages++;
			}
		}catch (Exception e1) {
			System.out.println("Exception: " + e1.toString());
		}catch (Throwable e2) {
			System.out.println("Export error: " + e2.toString());
		}
		return Integer.toString(NumPages);
	}

	public static ArrayList paginateStmtDtls(ArrayList arlOriDtlsDao,int maxPerPage, int pageNo) {
		//int maxPerPage = 10;
		ArrayList paginatedArl = new ArrayList();
		try{
			if(maxPerPage*pageNo<arlOriDtlsDao.size()){
				for (int index = maxPerPage*(pageNo-1); index < pageNo*maxPerPage; index++) {
					paginatedArl.add(arlOriDtlsDao.get(index));
				}
			}else{
				for (int index = maxPerPage*(pageNo-1); index < arlOriDtlsDao.size(); index++) {
					paginatedArl.add(arlOriDtlsDao.get(index));
				}
			}
		}catch (Exception e1) {
			System.out.println("GenUtil paginateStmtDtls Exception: " + e1.toString());
		}catch (Throwable e2) {
			System.out.println("GenUtil paginateStmtDtls Export error: " + e2.toString());
		}
		return paginatedArl;
	}
    public static String emptyStrToZero(String inStr) {
        if (inStr == null)
            return "0";
        else
            return inStr.trim();
    }


    private static String convertToHex(byte[] data) {
        StringBuffer buf = new StringBuffer();
        for (int i = 0; i < data.length; i++) {
            int halfbyte = (data[i] >>> 4) & 0x0F;
            int two_halfs = 0;
            do {
                if ((0 <= halfbyte) && (halfbyte <= 9))
                    buf.append((char) ('0' + halfbyte));
                else
                    buf.append((char) ('a' + (halfbyte - 10)));
                halfbyte = data[i] & 0x0F;
            } while(two_halfs++ < 1);
        }
        return buf.toString();
    }

    public static String MD5(String text)
    throws NoSuchAlgorithmException, UnsupportedEncodingException  {
        MessageDigest md;
        md = MessageDigest.getInstance("MD5");
        byte[] md5hash = new byte[32];
        md.update(text.getBytes("iso-8859-1"), 0, text.length());
        md5hash = md.digest();
        return convertToHex(md5hash);
    }

}	// GeneralUtility

//https://sample/payment.aspx?username=TEST&accid=123&statid=240280313&amt=100.00&Hash=914DE94FDBE8C6B40753EF1C8574630D
//MD5Input = hashing Salt +  userName + accid + statid + amt